﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acms.Bll;
using Acms.AcmsControls;
public partial class Templates_TabHolder_Display : Acms.AcmsControls.TemplateUserControl
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        dsTabChildren.SelectData += new EventHandler<Acms.DataControls.SelectEventArgs<Acms.Bll.Post>>(dsTabChildren_SelectData);
        dlTabTitle.ItemDataBound += new EventHandler<Acms.WebControls.ItemDataBoundEventArgs>(dlTabTitle_ItemDataBound);
        dsImages.SelectData += new EventHandler<Acms.DataControls.SelectEventArgs<Post>>(dsImages_SelectData);
        dlImageScroll.ItemDataBound += new EventHandler<Acms.WebControls.ItemDataBoundEventArgs>(dlImageScroll_ItemDataBound);
        
    }

    void dsImages_SelectData(object sender, Acms.DataControls.SelectEventArgs<Post> e)
    {
        var imageConainer = AcmsContext.CurrentPost.Children.FirstOrDefault(x => x.TemplateName == "NavigateFirstChild");
        if (imageConainer != null)
        {
            e.SetData(imageConainer.Children);
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataBind();
        }
    }

    void dsTabChildren_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.Post> e)
    {
        //e.SetData(AcmsContext.CurrentPost.Children.OrderBy(x => x.Sequence).ToList());
        e.SetData(AcmsContext.CurrentPost.Children.Where(x => x.TemplateName == "TabItem").ToList());
    }
    
    void dlTabTitle_ItemDataBound(object sender, Acms.WebControls.ItemDataBoundEventArgs e)
    {
        var post = e.DataItem as Post;
        if (post != null)
        {
            phTabs.Visible = true;
            //SET BACKGROUND IMAGE "BackgroundImage" pnlFlowImagesContainer
            
            //var pnlItem = e.Item.FindControl("pnlFlowImagesContainer") as Panel;
            //if (pnlItem != null)
            //{
                //pnlItem.BackImageUrl = "~/Images/dbImages/systems-1.jpg";
                //var control = acmsPage.AcmsContext.CurrentPost.FindPostControl("Backgound Image");

                var backGroundImage= post.FindPostControl("BackgroundImage");// as ImageContentControlDisplay;
                if (backGroundImage != null)
                {
                    var prop = backGroundImage.GetProperties<Acms.AcmsControls.ImageContentControlProperties>();
                    if (prop != null && prop.HasResource())
                    {
                        pnlFlowImagesContainer.BackImageUrl = Acms.AcmsControls.RenderFile.GetUrl(prop.ResourceId, this.Page);
                    }
                }
            //}
        }

    }

    void dlImageScroll_ItemDataBound(object sender, Acms.WebControls.ItemDataBoundEventArgs e)
    {
        var post = e.DataItem as Post;
        if (post != null)
        {
            //phTabs.Visible = true;
        }
    }


    public override void DataBind()
    {
        base.DataBind();
        var post = AcmsContext.CurrentPost;

        if (post != null)
        {
            var backGroundImage = post.FindPostControl("BackgroundImage");// as ImageContentControlDisplay;
            if (backGroundImage != null)
            {
                var prop = backGroundImage.GetProperties<Acms.AcmsControls.ImageContentControlProperties>();
                if (prop != null && prop.HasResource())
                {
                    pnlFlowImagesContainer.BackImageUrl = Acms.AcmsControls.RenderFile.GetUrl(prop.ResourceId, this.Page);
                }
            }
        }
    }
}
